﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using _411Client.HTML;
using Utility;
using File = Utility.File;
using Message = Utility.Message;

namespace _411Client
{
    public partial class ChatWindow : Form
    {
        //以下是字体相关信息
        private string _fontFamily = "微软雅黑";
        private int _fontSize = 12;
        private Color _fontColor = Color.Black;
        //服务器连接
        private readonly Connect.Connect _connect = new Connect.Connect();
        //聊天内容
        private HtmlPage _html = new HtmlPage();
        //接收文件的路径
        private string _receivePath;
        //发送文件的路径
        private string _sendPath;

        public ChatWindow()
        {
            InitializeComponent();

            lbFileName.Location = new Point(550, 50);
            pbFile.Location = new Point(550, 80);
            btnReceive.Location = new Point(750, 120);
            btnReject.Location = new Point(765, 120);
            btnCancel.Location = new Point(765, 120);
        }

        //文字颜色
        private void btnFontColor_Click(object sender, EventArgs e)
        {
            colorForFont.ShowDialog();
            _fontColor = colorForFont.Color;
            btnFontColor.BackColor = _fontColor;
            tbMessage.ForeColor = _fontColor;
            flpFeeling.Visible = false;
        }

        //发送消息
        private void btnSend_Click(object sender, EventArgs e)
        {
            if (tbMessage.Text != "")
            {
                _html.AddUserInfo(_connect.Me.Name, true);
                Message msg = new Message(tbMessage.Text, _fontFamily, _fontSize, _fontColor);
                _html.AddMessage(msg);
                _html.Save();
                tbMessage.Text = "";

                wbContent.Url = new Uri(_html.GetUrlString());

                _connect.SendMessage(msg);
            }
            else
            {
                Message msg = new Message("*消息不可以为空" + DateTime.Now, "微软雅黑", 12, Color.DimGray);
                _html.AddMessage(msg);
                _html.Save();

                wbContent.Url = new Uri(_html.GetUrlString());
            }
            flpFeeling.Visible = false;
            tbMessage.Focus();
        }

        //发送图片
        private void btnImage_Click(object sender, EventArgs e)
        {
            if (ofdImage.ShowDialog() == DialogResult.OK)
            {
                string imagePath = ofdImage.FileName;

                _html.AddUserInfo(_connect.Me.Name, true);
                _html.AddImage(imagePath);
                _html.Save();
                tbMessage.Text = "";

                wbContent.Url = new Uri(_html.GetUrlString());

                _connect.SendImage(imagePath);
            }
            flpFeeling.Visible = false;
            tbMessage.Focus();
        }

        //发送表情
        private void btnFeeling_Click(object sender, EventArgs e)
        {
            flpFeeling.Visible = !flpFeeling.Visible;
        }

        //传送文件
        private void btnFile_Click(object sender, EventArgs e)
        {
            if (ofdFile.ShowDialog() == DialogResult.OK)
            {
                _sendPath = ofdFile.FileName;
                ShowFileWindow(true, ofdFile.SafeFileName);
                _connect.SendFileRequest(ofdFile.SafeFileName);
            }
            flpFeeling.Visible = false;
            tbMessage.Focus();
        }

        //拒绝接受文件
        private void btnReject_Click(object sender, EventArgs e)
        {
            HideFileWindow();
            _connect.RejectFile();
            flpFeeling.Visible = false;
        }

        //取消传送文件
        private void btnCancel_Click(object sender, EventArgs e)
        {
            HideFileWindow();
            _connect.CancelFile();
        }

        //另存为
        private void btnReceive_Click(object sender, EventArgs e)
        {
            if(fbdReceive.ShowDialog() == DialogResult.OK)
            {
                _receivePath = fbdReceive.SelectedPath;
                _connect.PermitFile();
            }
            flpFeeling.Visible = false;
        }

        //显示发送接受文件的部分
        private void ShowFileWindow(bool isSend, string fileName)
        {
            Width += 200;
            lbFileName.Text = fileName;

            if (isSend)
            {
                btnCancel.Location = new Point(665, 120);
            }
            else
            {
                btnReceive.Location = new Point(550, 120);
                btnReject.Location = new Point(665, 120);
            }
            btnFile.Enabled = false;
        }

        //隐藏发送接受文件的部分
        private void HideFileWindow()
        {
            Width -= 200;

            btnReceive.Location = new Point(750, 120);
            btnReject.Location = new Point(765, 120);
            btnCancel.Location = new Point(765, 120);

            btnFile.Enabled = true;
        }

        //修改字号
        private void cbFontSize_TextChanged(object sender, EventArgs e)
        {
            _fontSize = int.Parse(cbFontSize.Text);
            tbMessage.Font = new Font(tbMessage.Font.Name, _fontSize);
            tbMessage.Focus();
            flpFeeling.Visible = false;
        }

        //修改字体
        private void cbFontFamily_TextChanged(object sender, EventArgs e)
        {
            _fontFamily = cbFontFamily.Text;
            tbMessage.Font = new Font(_fontFamily, tbMessage.Font.Size);
            tbMessage.Focus();
            flpFeeling.Visible = false;
        }

        //连接服务器
        private void btnConnect_Click(object sender, EventArgs e)
        {
            if(tbName.Text == "Your Name" || tbName.Text.Replace(" ","") == "")
            {
                MessageBox.Show("请输入一个昵称！");
                return;
            }
            tbName.Enabled = false;
            btnConnect.Enabled = false;
            int id = _connect.Connected(tbName.Text);
            if (id == 0)
            {
                tbName.Enabled = true;
                btnConnect.Enabled = true;
                MessageBox.Show("无法连接服务器！");
                return;
            }
            lbTitle.Text = "聊天 - 已经连接服务器";
            _connect.Me = new User(id.ToString(), tbName.Text);
        }

        //单击昵称时清空
        private void tbName_Click(object sender, EventArgs e)
        {
            if (tbName.Enabled)
            {
                tbName.Text = "";
            }
        }

        //断开连接
        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            _connect.Disconnected();
            tbName.Enabled = true;
            btnConnect.Enabled = true;
            btnDisconnect.Enabled = false;
            btnFontColor.Enabled = false;
            btnImage.Enabled = false;
            btnFeeling.Enabled = false;
            btnFile.Enabled = false;
            btnSend.Enabled = false;
            _html.AddMessage(new Message("*您已经断开连接  " + DateTime.Now, "微软雅黑", 12, Color.DimGray));
            _html.Save();
            wbContent.Url = new Uri(_html.GetUrlString());
            lbTitle.Text = "聊天";
            flpFeeling.Visible = false;
        }

        //播放声音
        private void PlaySound(string name)
        {
            SoundPlayer sp = new SoundPlayer("App_Sounds/"+name+".wav");
            sp.Play();
        }

        #region 供连接部分调用
        /// <summary>
        /// 连接上好友
        /// </summary>
        public void GetFriend()
        {
            btnDisconnect.Enabled = true;
            btnFontColor.Enabled = true;
            btnImage.Enabled = true;
            btnFeeling.Enabled = true;
            btnFile.Enabled = true;
            btnSend.Enabled = true;
            lbTitle.Text = "正在聊天 - " + _connect.MyFriend.Name;
            _html.AddMessage(new Message(string.Format("*帮您找到陌生人，您正在与{0}聊天  ", _connect.MyFriend.Name) + DateTime.Now,
                                         "微软雅黑", 12, Color.DimGray));
            _html.Save();
            wbContent.Url = new Uri(_html.GetUrlString());

            PlaySound("on");
        }

        /// <summary>
        /// 接受消息
        /// </summary>
        /// <param name="msg"></param>
        public void ReceiveMessage(Message msg)
        {
            _html.AddUserInfo(_connect.MyFriend.Name, true);
            _html.AddMessage(msg);
            _html.Save();

            wbContent.Url = new Uri(_html.GetUrlString());

            PlaySound("msg");
        }

        /// <summary>
        /// 接受图片
        /// </summary>
        /// <param name="img"></param>
        public void ReceiveImage(Images img)
        {
            _html.AddUserInfo(_connect.MyFriend.Name, true);
            _html.AddImage(img);
            _html.Save();

            wbContent.Url = new Uri(_html.GetUrlString());

            PlaySound("msg");
        }

        /// <summary>
        /// 传送文件请求
        /// </summary>
        public void ReceiveFileRequest(string fileName)
        {
            ShowFileWindow(false, fileName);
            _html.AddMessage(new Message(string.Format("*对方向您发送文件{0}，请确认安全后同意接受  ", fileName) + DateTime.Now, "微软雅黑", 12,
                                         Color.DimGray));
            _html.Save();
            wbContent.Url = new Uri(_html.GetUrlString());

            PlaySound("file");
        }

        /// <summary>
        /// 接受文件
        /// </summary>
        public void ReceiveFile(File file)
        {
            FileStream fs = System.IO.File.Create(_receivePath + file.Name + file.Extension);
            fs.Write(file.FileContent, 0, file.FileContent.Length);
            fs.Close();
            HideFileWindow();
            _html.AddMessage(new Message("*文件接受完毕  " + DateTime.Now, "微软雅黑", 12, Color.DimGray));
            _html.Save();
            wbContent.Url = new Uri(_html.GetUrlString());
        }

        /// <summary>
        /// 对方取消传送文件
        /// </summary>
        public void CancelFile()
        {
            HideFileWindow();
            _html.AddMessage(new Message("*对方取消传送文件  " + DateTime.Now, "微软雅黑", 12, Color.DimGray));
            _html.Save();
            wbContent.Url = new Uri(_html.GetUrlString());
        }

        /// <summary>
        /// 对方同意接受文件
        /// </summary>
        public void PermitFile()
        {
            _html.AddMessage(new Message("*对方同意接收文件，开始传送  " + DateTime.Now, "微软雅黑", 12, Color.DimGray));
            _html.Save();
            wbContent.Url = new Uri(_html.GetUrlString());
            _connect.SendFile(_sendPath);
            HideFileWindow();
        }

        /// <summary>
        /// 对方拒绝接受文件
        /// </summary>
        public void RejectFile()
        {
            HideFileWindow();
            _connect.DiconnectFileServerSocket();
            _html.AddMessage(new Message("*对方拒绝传送文件  " + DateTime.Now, "微软雅黑", 12, Color.DimGray));
            _html.Save();
            wbContent.Url = new Uri(_html.GetUrlString());
        }

        /// <summary>
        /// 被动断开连接
        /// </summary>
        public void Disconnect()
        {
            _connect.Disconnected();
            tbName.Enabled = true;
            btnConnect.Enabled = true; 
            btnDisconnect.Enabled = false;
            btnFontColor.Enabled = false;
            btnImage.Enabled = false;
            btnFeeling.Enabled = false;
            btnFile.Enabled = false;
            btnSend.Enabled = false;
            _html.AddMessage(new Message("*对方已经断开连接  " + DateTime.Now, "微软雅黑", 12, Color.DimGray));
            _html.Save();
            lbTitle.Text = "聊天";
            wbContent.Url = new Uri(_html.GetUrlString());

            PlaySound("off");
        }
        #endregion

        #region 窗体操作
        //拖动窗体
        private Point _mousePoint;
        private bool _canMove = false;
        private void ChatWindow_MouseDown(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left)
            {
                _mousePoint.X = e.X;
                _mousePoint.Y = e.Y;
                _canMove = true;
            }
        }
        private void ChatWindow_MouseMove(object sender, MouseEventArgs e)
        {
            if (_canMove)
            {
                Top = Control.MousePosition.Y - _mousePoint.Y;
                Left = Control.MousePosition.X - _mousePoint.X;
            }
        }
        private void ChatWindow_MouseUp(object sender, MouseEventArgs e)
        {
            _canMove = false;
        }

        //最小化
        private void btnMin_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
        }

        //关闭
        private void btnClose_Click(object sender, EventArgs e)
        {
            _html.DeleteChatFiles();
            _connect.Disconnected();
            Close();
            Application.Exit();
        }
        #endregion 

        #region 表情
        private void DoClick()
        {
            flpFeeling.Visible = false;
            tbMessage.Focus();
            tbMessage.SelectionStart = tbMessage.Text.Length;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x1\x1";
            DoClick();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x1\x2"; 
            DoClick();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x1\x3";
            DoClick();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x1\x4";
            DoClick();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x1\x5";
            DoClick();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x1\x6";
            DoClick();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x1\x7";
            DoClick();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x1\x8";
            DoClick();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x2\x1";
            DoClick();
        }

        private void button10_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x2\x2";
            DoClick();
        }

        private void button11_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x2\x3";
            DoClick();
        }

        private void button12_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x2\x4";
            DoClick();
        }

        private void button13_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x2\x5";
            DoClick();
        }

        private void button14_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x2\x6";
            DoClick();
        }

        private void button15_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x2\x7";
            DoClick();
        }

        private void button16_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x2\x8";
            DoClick();
        }

        private void button17_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x3\x1";
            DoClick();
        }

        private void button18_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x3\x2";
            DoClick();
        }

        private void button19_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x3\x3";
            DoClick();
        }

        private void button20_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x3\x4";
            DoClick();
        }

        private void button21_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x3\x5";
            DoClick();
        }

        private void button22_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x3\x6";
            DoClick();
        }

        private void button23_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x3\x7";
            DoClick();
        }

        private void button24_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x3\x8";
            DoClick();
        }

        private void button25_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x4\x1";
            DoClick();
        }

        private void button26_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x4\x2";
            DoClick();
        }

        private void button27_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x4\x3";
            DoClick();
        }

        private void button28_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x4\x4";
            DoClick();
        }

        private void button29_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x4\x5";
            DoClick();
        }

        private void button30_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x4\x6";
            DoClick();
        }

        private void button31_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x4\x7";
            DoClick();
        }

        private void button32_Click(object sender, EventArgs e)
        {
            tbMessage.Text += "\x4\x8";
            DoClick();
        }
#endregion

        private void tbMessage_Click(object sender, EventArgs e)
        {
            flpFeeling.Visible = false;
        }

        private void ChatWindow_Click(object sender, EventArgs e)
        {

            flpFeeling.Visible = false;
        }
    }
}
